package com.gx.mxbbx.controller;

import com.github.pagehelper.PageInfo;
import com.gx.mxbbx.po.Area;
import com.gx.mxbbx.po.Roles;
import com.gx.mxbbx.po.SysUser;
import com.gx.mxbbx.service.RolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
public class RolesController {
    @Autowired
    private RolesService rolesService;

    //角色管理
    @RequestMapping("/getRolesList")
    public String getRolesList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String roleName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<Roles> pageInfo = rolesService.getRolesList(roleName, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", roleName);
        return "/roles/roles_list";
    }

    @RequestMapping("/openroleAdd")
    public String openroleAdd(Model model) {
        return "/roles/roles_add";
    }

    //增加角色
    @RequestMapping("/getroleadd")
    public String getroleadd(Roles roles, HttpServletRequest request, Model model) {
        rolesService.getroleadd(roles);
        return getRolesList(1, 10, request, model);
    }

    //删除
    @RequestMapping("/roledelete")
    public String roledelete(Integer id, HttpServletRequest request, Model model) {
        rolesService.roledelete(id);
        return getRolesList(1, 10, request, model);
    }

    @RequestMapping("/openroleedit")
    public String openroleedit(Integer id, Model model) {
        Roles roles = rolesService.getroleById(id);
        model.addAttribute("user", roles);
        return "/roles/roles_edit";
    }

    //修改角色信息
    @RequestMapping("/rolesedit")
    public String rolesedit(Roles roles, HttpServletRequest request, Model model) {
        rolesService.rolesedit(roles);
        return getRolesList(1, 10, request, model);
    }
}
